package me.pplay.playtalk.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.adapter.CommonAdapter;
import me.pplay.playtalk.bean.CommonControlID;
import me.pplay.playtalk.callback.CommonAdapterOnClickCallBack;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.protocol.DataCallBack;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.TpChatRoom;
import me.pplay.playtalk.protocol.bean.RspData;
import me.pplay.playtalk.protocol.bean.TpGroup;
import me.pplay.playtalk.service.PlayTalkService;
import me.pplay.playtalk.util.LogManager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

public class CommunityActivity extends BaseActivity {
	private List<Map<String, Object>> mData;
	private ListView list;
	private LinkedList<TpGroup> roomlist;
	private TpChatRoom tpChatRoom;
	private List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
	private ProgressDialog progressDialog;
	private CommonAdapter adapter;
	private ImageButton btn_back;
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case TpChat.CHAT_ROOM_GETLIST_OK:
				adapter.notifyDataSetChanged();
				break;
			case TpChatRoom.CHATROOM_REQ_ERR : {
				progressDialog.dismiss();
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "Unkown err" : rspdata.getString());
			}
				break;
			case TpChatRoom.CHATROOM_REQ_TIMEOUT :
				progressDialog.dismiss();
				showToast("Time out");
				break;
			case TpChatRoom.CHATROOM_REQ_OK:
				progressDialog.dismiss();
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("from", "community");
				bundle.putSerializable("tpgroup", (TpGroup) msg.obj);
				intent.putExtras(bundle);
				intent.setClass(CommunityActivity.this, ChatConversationActivity.class);
				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_community);
		super.SetOnBackClickListener((ImageButton)findViewById(R.id.btn_back));
		mData = getData();

		CommonControlID holderId = new CommonControlID();
		holderId.itemLayout = R.layout.layout_communitylist;
		holderId.leftImgId = R.id.community_img;
		holderId.rightImgId = R.id.community_chat;
		holderId.leftStrId = R.id.community_title;
		holderId.leftStr1Id = R.id.community_topic;
		adapter = new CommonAdapter(CommunityActivity.this, mData, holderId, new CommonAdapterOnClickCallBack() {

			@Override
			public void OnClickCallback(int pos, int ViewId, View v) {
				// TODO Auto-generated method stub
				LogManager.LogShow("CommunityActivity", "OnClickCallback position " + pos);

			}
		});
		list = (ListView) this.findViewById(R.id.community_list); // 联系人ListView
		list.setAdapter(adapter);// 将数据适配器与Activity进行绑定
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		tpChatRoom = appState.getTpChatRoom();
		btn_back	= (ImageButton)findViewById(R.id.btn_back);
		clickEvents();
	}

	private List<Map<String, Object>> getData() {
		listData.clear();
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		TpChat tpChat = appState.getTpChat();
		roomlist = tpChat.tp_chat_get_chatroom_data();
		if (roomlist != null) {
			for (TpGroup g : roomlist) {
				LogManager.LogShow("CommunityActivity", "g.getName() " + g.getName());
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("leftStr", g.getName());
				if (g.getAlias() != null)
					map.put("leftStr1", g.getAlias());
				map.put("leftImg", R.drawable.community_default);
				map.put("rightImg", R.drawable.commnutiy_chat);
				listData.add(map);

			}
		}
		return listData;
	}

	private void clickEvents() {
		btn_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				CommunityActivity.this.finish();
			}
		});
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				LogManager.LogShow("CommunityActivity", "tp_chatroom_req_addchatroom alias:");
				final TpGroup tpGroup = roomlist.get(arg2);
				if (tpGroup == null)
					return;
				String alias = tpGroup.getAlias();
				if (alias == null)
					alias = tpGroup.getId() + "";
				progressDialog = ProgressDialog.show(CommunityActivity.this, getString(R.string.loading), getString(R.string.waittng), true, false); 
				progressDialog.show();
				LogManager.LogShow("CommunityActivity", "tp_chatroom_req_addchatroom alias:" + alias + "  skyid:" + Globals.skyid);
				tpChatRoom.tp_chatroom_req_addchatroom(alias, Globals.skyid, tpGroup.getId(), new DataCallBack() {
 
					@Override
					public void Callback(int result, RspData resultdata, Object obj) {
						LogManager.LogShow("CommunityActivity", "tp_chatroom_req_addchatroom result:" + result); // 208
																													// true
						// TODO Auto-generated method stub
						Message msg = new Message(); 
						msg.what = result; 
						msg.obj = resultdata;
						if (TpChatRoom.CHATROOM_REQ_OK == result) {
							msg.obj = tpGroup;
						} else if (TpChatRoom.CHATROOM_REQ_ERR == result) {
						} else if (TpChatRoom.CHATROOM_REQ_TIMEOUT == result) {
						} 
						handler.sendMessage(msg);
					}
				});
			}
		});

	}

	@Override
	protected void onStart() {
		PlayTalkService.getInstance().setHandler(handler);
		super.onStart();
	}

	@Override
	protected void onStop() {
		PlayTalkService.getInstance().setHandler(null);
		super.onStop();
	}
}
